package leetcode.code0482;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution428 implements IDebug {
	public String licenseKeyFormatting(String s, int k) {
		int per = k;// 每段数量
		StringBuffer sb = new StringBuffer();
		for (int i = s.length() - 1; i >= 0; i--) {
			char c = s.charAt(i);// 当前处理字符
			if (c == '-')// `-`号不处理
				continue;
			if (c >= 97) {// 小写字母转大写
				c = (char) (c - 32);
			}
			if (per > 0) {// 当前段还有数量则追加
				sb.append(c);
				per--;
			} else {
				sb.append('-');// 当前段结束,追加`-`号
				sb.append(c);// 添加当前符号
				per = k - 1;// 当前端仍需字母数量
			}
		}
		return sb.reverse().toString();//尾部长,头部短,倒着处理完毕后,逆序
	}

	@Override
	public void debug1() {
		String s = "5F3Z-2e-9-w";
		int k = 4;
		String e = "5F3Z-2E9W";
		String r = this.licenseKeyFormatting(s, k);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug2() {
		String s = "2-5g-3-J";
		int k = 2;
		String e = "2-5G-3J";
		String r = this.licenseKeyFormatting(s, k);
		HelpDebug.compare(e, r);
	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution428 so = new Solution428();
		so.debug1();
		so.debug2();
		System.out.println((int) 'a');
		System.out.println((int) 'z');
		System.out.println((int) 'A');
		System.out.println((int) 'Z');
	}

}
